public class 验证回文串II {
    //https://leetcode.cn/problems/RQku0D/description/

    //成功的方法,贪心加字符串模拟
    public boolean validPalindrome(String s) {
        //双指针判断是否为回文串
        int left = 0;
        int right = s.length() - 1;
        while(left < right){
            //只有一次机会, 跳过左字符或跳过右字符
            if(s.charAt(left) != s.charAt(right)){
                return judge(s,left + 1,right) || judge(s,left,right - 1);
            }
            left++;
            right--;
        }
        return true;
    }
    public static boolean judge(String s,int l,int r){
        while(l < r){
            //机会已经用完了, 再失败直接返回失败
            if(s.charAt(l) != s.charAt(r)){
                return false;
            }
            l++;
            r--;
        }
        return true;
    }

    //失败的暴力解法
    public boolean validPalindrome1(String s) {
        //判断不删是否是回文串
        if(set(s)){
            return true;
        }else{
            for(int i = 0;i < s.length();i++){
                StringBuilder sb = new StringBuilder(s);
                sb.deleteCharAt(i);
                if(set(sb.toString())){
                    return true;
                }
            }
        }
        return false;
    }
    public static boolean set(String s){
        int cur = (s.length() & 1);
        if(cur == 1){
            int left = s.length() / 2;
            int right = s.length() / 2;
            while(left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)){
                left--;
                right++;
            }
            if(left == -1 && right == s.length()){
                return true;
            }
        }else{
            int left = s.length() / 2 - 1;
            int right = s.length() / 2;
            while(left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)){
                left--;
                right++;
            }
            if(left == -1 && right == s.length()){
                return true;
            }
        }
        return false;
    }
}
